package com.dormitory.ui;

import com.dormitory.model.User;
import com.dormitory.service.UserService;
import com.dormitory.service.impl.UserServiceImpl;

import javax.swing.*;
import java.awt.*;

public class RegisterFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JComboBox<String> roleComboBox;
    private UserService userService;

    public RegisterFrame() {
        userService = new UserServiceImpl();
        initUI();
    }

    private void initUI() {
        setTitle("宿舍管理系统 - 注册");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 创建标题面板
        JPanel titlePanel = new JPanel();
        JLabel titleLabel = new JLabel("用户注册");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titlePanel.add(titleLabel);
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // 创建表单面板
        JPanel formPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 用户名
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel usernameLabel = new JLabel("用户名：");
        usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 1.0;
        usernameField = new JTextField(20);
        usernameField.setPreferredSize(new Dimension(200, 30));
        formPanel.add(usernameField, gbc);

        // 密码
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.0;
        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 1.0;
        passwordField = new JPasswordField(20);
        passwordField.setPreferredSize(new Dimension(200, 30));
        formPanel.add(passwordField, gbc);

        // 确认密码
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0.0;
        JLabel confirmPasswordLabel = new JLabel("确认密码：");
        confirmPasswordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(confirmPasswordLabel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 1.0;
        confirmPasswordField = new JPasswordField(20);
        confirmPasswordField.setPreferredSize(new Dimension(200, 30));
        formPanel.add(confirmPasswordField, gbc);

        // 角色选择
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.weightx = 0.0;
        JLabel roleLabel = new JLabel("角色：");
        roleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(roleLabel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 1.0;
        String[] roles = {"管理员", "宿管员", "辅导员"};
        roleComboBox = new JComboBox<>(roles);
        roleComboBox.setPreferredSize(new Dimension(200, 30));
        roleComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(roleComboBox, gbc);

        mainPanel.add(formPanel, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JButton registerButton = new JButton("注册");
        JButton backButton = new JButton("返回登录");

        // 设置按钮样式
        Dimension buttonSize = new Dimension(100, 35);
        registerButton.setPreferredSize(buttonSize);
        backButton.setPreferredSize(buttonSize);
        registerButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        backButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 添加按钮事件
        registerButton.addActionListener(e -> handleRegister());
        backButton.addActionListener(e -> openLoginFrame());

        buttonPanel.add(registerButton);
        buttonPanel.add(backButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // 添加回车键监听
        getRootPane().setDefaultButton(registerButton);

        add(mainPanel);
    }

    private void handleRegister() {
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String role = (String) roleComboBox.getSelectedItem();

        if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
            JOptionPane.showMessageDialog(this, 
                "所有字段都必须填写！", 
                "提示", 
                JOptionPane.WARNING_MESSAGE);
            return;
        }

        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, 
                "两次输入的密码不一致！", 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
            passwordField.setText("");
            confirmPasswordField.setText("");
            passwordField.requestFocus();
            return;
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setUserRole(role);

        if (userService.register(user)) {
            JOptionPane.showMessageDialog(this, 
                "注册成功！", 
                "成功", 
                JOptionPane.INFORMATION_MESSAGE);
            openLoginFrame();
        } else {
            JOptionPane.showMessageDialog(this, 
                "注册失败，用户名可能已存在！", 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
            usernameField.setText("");
            passwordField.setText("");
            confirmPasswordField.setText("");
            usernameField.requestFocus();
        }
    }

    private void openLoginFrame() {
        new LoginFrame().setVisible(true);
        dispose();
    }
} 